var express = require('express');
var router = express.Router();
var dbconnect=require('../models/dbconnect.js')

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('indexad', { username:req.session.username, });
});

router.get('/indexUserSum',function(req,res){
  dbconnect('user').then(function({coll,dbCon}){
    coll.find({}).toArray(function(error,data){
      console.log(data.length)
      res.json({
        userNum:data.length
      })
    })
  })

})

router.get('/indexarticleSum',function(req,res){
  dbconnect('article').then(function({coll,dbCon}){
    coll.find({}).toArray(function(error,data){
      console.log(data.length)
      res.json({
        articleNum:data.length
      })
    })
  })

})
//查询用户接口
router.get('/searchUser',function(req,res){
  dbconnect('user').then(function({coll,dbCon}){
    coll.find({}).toArray(function(error,data){
      //console.log(data)
      console.log(data[0]._id)
      res.json({
        seachUser:data,
      })
    })
  })
})
//删除用户接口
router.post('/deleU',function(req,res){
  var ObjectId=require('mongodb').ObjectId
  var deleId=new ObjectId(req.body.id)
 // console.log(req.param.id)
 // console.log(deleId)
  dbconnect('user').then(function({coll,dbCon}){
    coll.remove({"_id":deleId},function(error,data){
        if (error) throw error;
       // res.json(data)
        res.json({
          code:1,
          msg:'删除成功'
        })
    })
  })
})
//查询文章接口
router.get('/searchAtic',function(req,res){
  dbconnect('article').then(function({coll,dbCon}){
    coll.find({}).toArray(function(error,data){
        if(error) throw error;
        res.json({
          Allarcticle:data
        })

    })
  })
})

//删除文章接口

router.post('/deleArticle',function(req,res){
  var ObjectId=require('mongodb').ObjectId
  var deleId=new ObjectId(req.body.id)
 // console.log(req.param.id)
 // console.log(deleId)
  dbconnect('article').then(function({coll,dbCon}){
    coll.remove({"_id":deleId},function(error,data){
        if (error) throw error;
       // res.json(data)
        res.json({
          code:1,
          msg:'删除成功'
        })
    })
  })
})
//读取文章的接口
router.post('/readArc',function(req,res){
  var ObjectId=require('mongodb').ObjectId
  var readId=new ObjectId(req.body.id)
  dbconnect('article').then(function({coll,dbCon}){
    coll.find({"_id":readId}).toArray(function(error,data){
        if(error) throw error;
        res.json({
          data:data
        })
       
    })
  })


})
// 修改文章接口

router.post('/uploadArc',function(req,res){
    var ObjectId=require('mongodb').ObjectId
    var upId=new ObjectId(req.body.id)
    var uptitle=req.body.arctitle
    var upcontent=req.body.content
    console.log('1111')
    console.log(upcontent,uptitle)
    //db.user.update({name:'jack'},{$set:{'school.name':'shida','school.city':'beijing'}})
    let time = new Date()
  let uptimestr = `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()}  ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`
  var updateArc={$set:{'arctitle':uptitle,'content':upcontent,'pubtime':uptimestr }}  
  dbconnect('article').then(function({coll,dbCon}){
      coll.updateOne({'_id':upId},updateArc,function(error,data){
        if(error) throw error;
        res.json({
          data:data
        })
      })
      
    })
})

//搜索用户的接口
router.post('/findUser',function(req,res){
  var findVal=req.body.findVal
  var  fVal=new RegExp(findVal)
  dbconnect('user').then(function({coll,dbCon}){
    coll.find({'username':fVal}).toArray(function(error,data){
      res.json({
        data:data
      })

    })
  })
  
})
//搜索文章的接口
router.post('/findArcticle',function(req,res){
  var findVal=req.body.findVal
  var  fVal=new RegExp(findVal)
  dbconnect('article').then(function({coll,dbCon}){
    coll.find({'arctitle':fVal}).toArray(function(error,data){
      res.json({
        data:data
      })

    })
  })

})






module.exports = router;
